﻿using System;
using System.Linq;
using Shell.DomainDot;
using System.Web.Mvc;
using Framework.Poco;
using Framework.Domain;
using System.Collections.Generic;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 友情链接控制器
    /// </summary>
    public class FriendlyLinkController : Controller
    {
        private IDomainDotFactory factory;

        /// <summary>
        /// 创建器
        /// </summary>
        public FriendlyLinkController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 友情链接创建
        /// </summary>
        [HttpGet]
        public ActionResult AgencyGroupCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<GlobalContext>();
                var model = new FriendlyLinkCreateViewModel();
                model.AgencyGroups = context.GetAllAgencyGroups();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 友情链接创建
        /// </summary>
        [HttpPost]
        public ActionResult AgencyGroupCreate(User currentUser, FriendlyLinkCreateViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var groupContext = factory.Create<AgencyGroupFriendlyLinkContext>(model.AgencyGroupIdentity);
                groupContext.Create(model.Instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 文章分类下的友情链接创建
        /// </summary>
        [HttpGet]
        public ActionResult CategoryCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<SelectCategory>();
                var model = new FriendlyLinkCreateViewModel();
                model.Categories = context.GetAllCategories();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 文章分类下的友情链接创建
        /// </summary>
        [HttpPost]
        public ActionResult CategoryCreate(User currentUser, FriendlyLinkCreateViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var categoryContext = factory.Create<CategoryFriendlyLinkContext>(model.CategoryIdentity);
                categoryContext.Create(model.Instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 全局友情链接创建
        /// </summary>
        [HttpGet]
        public ActionResult GlobalCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                return View();
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 全局友情链接创建
        /// </summary>
        [HttpPost]
        public ActionResult GlobalCreate(User currentUser, FriendlyLinkCreateViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<FriendlyLinkContext>();
                context.Create(model.Instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 友情链接删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(User currentUser, string name, Guid friendlyLinkId)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var globalContext = factory.Create<GlobalContext>();
                var agencyGroup = globalContext.FindAgencyGroup(name);
                var categoryContext = factory.Create<SelectCategory>();
                var category = categoryContext.FindCategory(name);
                var friendlyLinkContext = factory.Create<FriendlyLinkContext>();
                var model = new FriendlyLinkDeleteViewModel();
                if (agencyGroup != null)
                {
                    model.AgencyGroupIdentity = agencyGroup.Id;
                }
                if (category != null)
                {
                    model.CategoryIdentity = category.Id;
                }
                model.FriendlyLinkIdentity = friendlyLinkContext.GetAllFriendlyLinks().SingleOrDefault(fl => fl.Id == friendlyLinkId).Id;
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 友情链接删除
        /// </summary>
        [HttpPost]
        public ActionResult Delete(User currentUser, FriendlyLinkDeleteViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (model.CategoryIdentity != Guid.Empty)
                {
                    var context = factory.Create<CategoryFriendlyLinkContext>(model.CategoryIdentity);
                    context.Delete(model.FriendlyLinkIdentity);
                }
                if (model.AgencyGroupIdentity != Guid.Empty)
                {
                    var context = factory.Create<AgencyGroupFriendlyLinkContext>(model.AgencyGroupIdentity);
                    context.Delete(model.FriendlyLinkIdentity);
                }
                var advertisingContext = factory.Create<FriendlyLinkContext>();
                advertisingContext.Delete(model.FriendlyLinkIdentity);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 友情链接列表
        /// </summary>
        public ActionResult List(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<FriendlyLinkContext>();
                var model = new FriendlyLinkListViewModel();
                model.List = context.GetAllFriendlyLinks();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }
    }
}
